package com.woniu108.person.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.exception.WoniuException;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.carService.commos.util.StaticData;
import com.woniu108.person.model.AdminInfo;
import com.woniu108.person.model.AdminRole;
import com.woniu108.person.service.AdminInfoService;
import com.woniu108.person.service.AdminRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 管理员权限表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/adminRole")
@Api(tags = "管理员权限接口")
public class AdminRoleController {

    @Resource
    private AdminRoleService adminRoleService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private AdminInfoService adminInfoService;
    /**
     * 新增管理员权限名称
     * @param adminRoleName 管理员权限名称
     * @return
     */
    @GetMapping("/addAdminRole.api")
    @ApiOperation("新增管理员权限名称")
    @AdminHistoryLog
    @ApiImplicitParam(name = "adminRoleName",value = "管理员权限名称")
    public Result addAdminRole(String adminRoleName, @RequestHeader String token){
        String refreshToken=redisUtil.getString(token);
        String adminId= JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        AdminInfo adminInfo = adminInfoService.getById(adminId);
        if(ObjectUtil.isEmpty(adminInfo)||adminInfo.getAdminState()!= StaticData.ADMIN_STATE_NORMAL||adminInfo.getAdminRoleId()!=1){
            throw new WoniuException("无权新增",10001);
        }
        QueryWrapper<AdminRole> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_role_name",adminRoleName);
        List<AdminRole> list = adminRoleService.list(wrapper);
        if(ObjectUtil.isNotEmpty(list)) throw new WoniuException("管理员权限名称已存在",10001);
        AdminRole role = new AdminRole();
        role.setAdminRoleName(adminRoleName);
        adminRoleService.save(role);
        return Result.getResult().setMsg("添加管理员权限成功");
    }

    /**
     *修改管理员权限名称
     * @param adminRoleId
     * @param adminRoleName
     * @param token
     * @return
     */
    @GetMapping("/changeAdminRoleName.api")
    @ApiOperation("修改管理员权限名称")
    @AdminHistoryLog
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminRoleId",value = "管理员权限编号"),
            @ApiImplicitParam(name = "adminRoleName",value = "管理员权限名称")
    })
    public Result changeAdminRoleName(Integer adminRoleId,String adminRoleName,@RequestHeader String token){
        String refreshToken=redisUtil.getString(token);
        String adminId= JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        AdminInfo adminInfo = adminInfoService.getById(adminId);
        if(ObjectUtil.isEmpty(adminInfo)||adminInfo.getAdminState()!= StaticData.ADMIN_STATE_NORMAL||adminInfo.getAdminRoleId()!=1){
            throw new WoniuException("无权新增",10001);
        }
        QueryWrapper<AdminRole> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_role_name",adminRoleName);
        List<AdminRole> list = adminRoleService.list(wrapper);
        if(ObjectUtil.isNotEmpty(list)) throw new WoniuException("管理员权限名称已存在",10001);
        AdminRole adminRole = adminRoleService.getById(adminRoleId);
        if(adminRoleName!=null&&adminRoleName!=""){
            adminRole.setAdminRoleName(adminRoleName);
            adminRoleService.updateById(adminRole);
        }
        return Result.getResult().setMsg("修改管理员权限名称成功");
    }

    /**
     * 获取管理员权限列表
     * @return List<AdminRole> 管理员权限列表
     */
    @GetMapping("/adminRoleList.api")
    @ApiOperation("获取管理员权限列表")
    public Result<List<AdminRole>> adminRoleList(){
        List<AdminRole> list = adminRoleService.list();
        return Result.newListInstance(AdminRole.class).setData(list);
    }
}

